package service;

import java.util.List;
import java.util.Map;

import beans.Info;
import beans.Shop;
import dao.DAO;

public class ShopService {
	DAO<Shop> dao = null;
	public static final int PERPAGE = 15;

	public ShopService() {
		dao = new DAO<>(Shop.class);
	}

	/**
	 * 获取所有商铺信息
	 * 
	 * @param page
	 * @return
	 */
	public Info<Shop> getShop(String page) {
		int startP = (Integer.parseInt(page) - 1) * PERPAGE; // 分页开始条数
		Map<Long, List<Shop>> map = dao.getAll(startP, PERPAGE);
		Info<Shop> info = new Info<>();
		info.count = map.keySet().iterator().next();
		info.list = map.values().iterator().next();
		return info;
	}

	/**
	 * 删除商铺
	 * 
	 * @param id
	 */
	public void delShop(String id) {
		dao.delById(Integer.valueOf(id));
	}

	/**
	 * 根据id获取商铺信息
	 * 
	 * @param id
	 * @return
	 */
	public Shop getShopById(String id) {
		return dao.getById(Integer.parseInt(id));
	}

	/**
	 * 修改商铺信息
	 * 
	 * @param sid
	 * @param name
	 * @param tel
	 * @param address
	 * @param status
	 * @param describe
	 */
	public void alterShop(String sid, String name, String tel, String address, String status, String describe) {
		String hql = "update Shop set name='" + name + "', tel='" + tel + "', address='" + address
				+ "', status='" + status + "', shopDescribe='" + describe + "' where id=" + sid;
		dao.update(hql);
	}
	
	/**
	 * 添加商铺信息
	 * 
	 * @param name
	 * @param tel
	 * @param address
	 * @param status
	 * @param describe
	 */
	public void addShop(String name, String tel, String address, String status, String describe) {
		Shop shop = new Shop(0, name, tel, address, status, describe);
		dao.add(shop);
	}
	
	
}
